from flask import Flask
from config import config_map
from flask_sqlalchemy import SQLAlchemy

# 创建SQLAlchemy实例
db = SQLAlchemy()


def create_app(config_name):
    # 创建Flask实例
    app = Flask(__name__)
    Config = config_map.get(config_name)
    # 根据类加载配置数据
    app.config.from_object(Config)
    # 初始化db
    db.init_app(app)
    # 获取蓝图对象
    from flask_shop.user import user_bp

    # 注册蓝图
    app.register_blueprint(user_bp)

    # 获取menu对象
    from flask_shop.menu import menu_bp

    # 注册蓝图
    app.register_blueprint(menu_bp)

    # 获取menu对象
    from flask_shop.role import role_bp

    # 注册蓝图
    app.register_blueprint(role_bp)

    # 获取categories对象
    from flask_shop.category import cate_bp

    # 注册蓝图
    app.register_blueprint(cate_bp)

    # 获取categories对象
    from flask_shop.category import attr_bp

    # 注册蓝图
    app.register_blueprint(attr_bp)

    # 获取product对象
    from flask_shop.product import product_bp

    # 注册蓝图
    app.register_blueprint(product_bp)

    # 获取order对象
    from flask_shop.order import order_bp

    # 注册蓝图
    app.register_blueprint(order_bp)

    # 返回flask实例
    return app
